<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * The Profile class for table 'profile'.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.3
 * 
 * 0.11) Adds the column 'pinyin'.
 * 0.2)  Adds the 'getIdsByList' function.
 * 0.3)  Adds the self identity column 'id'. (primary auto_increment).
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
require_once INSTALLDIR . '/classes/User.php';

require_once INSTALLDIR . '/lib/slh_util.php';
require_once INSTALLDIR . '/lib/slh_cache.php';
require_once INSTALLDIR . '/lib/slh_arraywrapper.php';

class Profile extends Memcached_DataObject
{
	public $__table = 'profile';			// table name
	public $id;								// int(11)  primary_key not_null foreign_key
	public $user_id;						// int(11)  foreign_key unique not_null
	public $realname;						// varchar(255)  unique not_null
	public $pinyin;							// varchar(255)
	public $department;						// varchar(255)  not_null
	public $profile_rights;					// int(9)  unsigned not_null
	public $modified;						// timestamp()  not_null
	
	// Static get
	function staticGet($key, $value = null)
	{
		return Memcached_DataObject::staticGet('Profile', $key, $value); 
	}
	
	/**
	 * Get the user object.
	 * 'profile'.'user_id' => 'user'.'id'
	 */
	function getUser()
	{
		return User::staticGet('id', $this->user_id);
	}
	
	/**
	 * Adds a new profile account in the table 'profile'.
	 * 
	 * Before doing this, account must be inserted correctly in the 
	 * table 'user'. So the 'user_id' should be correct.
	 * 
	 * @return The Profile id.
	 */
	static function add($fields)
	{
		// Magically put fields into current scope.
		extract($fields);
		
		$user = new User();
		$user->status = USER_STATUS_VERIFIED;
		$user->username = $username;
		$user->password = md5($username);
		$user->type = USER_TYPE_PROFILE;
		$user->created = slh_sql_now();
		
		// BEGIN
		$user->query('BEGIN');
		
		$user_id = $user->insert();
		if (empty($user_id)) {
			slh_log_db_error($user, 'INSERT', __FILE__);
			return false;
		}
		$user->user_id = $user_id;
		$profile = new Profile();
		$profile->user_id = $user_id;
		$profile->realname = $realname;
		$profile->pinyin = strtolower($pinyin);
		$profile->department = $department;
		$profile->profile_rights = $profile_rights;
		
		$result = $profile->insert();
		if (empty($result)) {
			slh_log_db_error($profile, 'INSERT', __FILE__);
			return false;
		}
		
		// COMMIT
		$user->query('COMMIT');
		
		return $user_id;
	}
	
	/**
	 * Get the ids as a list. Use offset and limit to control
	 * the pagination.
	 */
	static function getUseridsByList($offset = 0,
								 	 $limit = 0)
	{
		$profile = new Profile();
		
		$profile->selectAdd();
		$profile->selectAdd('user_id');
		
		$profile->whereAdd('user_id in (SELECT `id` FROM `user`)');
		
		$profile->orderBy('user_id ASC');
		
		if ( !is_null($offset)) {
			$profile->limit($offset, $limit);
		}
		
		$userids = array();
		if ($profile->find()) {
			while ($profile->fetch()) {
				$userids[] = $profile->user_id;
			}
		}
		
		$profile->free();
		$profile = NULL;
		
		return $userids;
	}
	
	/**
	 * Get the special ids of the profile users.
	 * 
	 * Usually the list with pagination.
	 */
	static function getProfilesByUserids($userids)
	{
		$cache = Cache::instance();
		
		if ( !empty($cache)) {
			$profiles = array();
			foreach ($userids as $id) {
				$p = Profile::staticGet('user_id', $id);
				if ( !empty($p)) {
					$profiles[] = $p;
				}
			}
			
			return new ArrayWrapper($profiles);
		} else {
			// Miss the cache.
			$profiles = new Profile();
			if (empty($userids)) {
				return $profiles;
			}
			
			$profiles->whereAdd('user_id in (' . implode(', ', $userids) . ')');
			$profiles->find();
			
			$temp = array();
			while ($profiles->fetch()) {
				$temp[$profiles->id] = clone($profiles);
			}
			
			$wrapped = array();
			
			foreach($userids as $id) {
				if (array_key_exists($id, $temp)) {
					$wrapped[] = $temp[$id];
				}
			}
			
			return new ArrayWrapper($wrapped);
		}
	}
}
?>